package com.appcan.base;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;


public class BaseDAO<T> extends UfidaDAO {

	public BaseDAO(EntityManager manager) {
		super(manager);
	}
	public void saveEntityList(List<T> messagelist){
		for (int i=0; i<messagelist.size(); i++) {
			this.getManager().persist(messagelist.get(i));
			if (i % 10 == 0) {
				this.getManager().flush();
			}
		}
		this.getManager().flush();
	}
	public void saveEntry(T t) {
		this.getManager().persist(t);
	}
	public void updateEntry(T t) {
		this.getManager().merge(t);
	}
	public void updatesql(String sql){
		getManager().createNativeQuery(sql.toString()).executeUpdate();
	}
	public void deletesql(String sql){
		getManager().createNativeQuery(sql.toString()).executeUpdate();
	}
	public int validate(String sql){
		List<Object[]> resultlist = this.getManager().createNativeQuery(sql.toString()).getResultList();
		if(resultlist==null||resultlist.size()<=0)return 0;
		return 1;	
	}
	public T getEntityByHql(String hql) {
		T entity = null;
		Query query = this.getManager().createQuery(hql);
		List<T> result = query.getResultList();
		if (result != null && result.size() > 0) entity = result.get(0);
		return entity;
	}
	
	public List<T> qyeryEntityByHql( String hql) {
		Query query = this.getManager().createQuery(hql);
		List<T> resultlist = query.getResultList();
		return resultlist;
	}
	public List<Object[]> executeSql(String sql) {
		Query query = this.getManager().createNativeQuery(sql);
		List<Object[]> resultlist = query.getResultList();
		return resultlist;
	}
	
	/**
	 * 获取记录总数
	 * @param hql
	 * @return
	 */
	public int getCountByHql(String hql) {
		Query query = this.getManager().createQuery(hql);
		Object srobj = query.getSingleResult();
		 int len = tranfer(srobj);
		return len;
	}
	
	/**
	 * 获取分页获取记录
	 * @param firstresult
	 * @param maxresult
	 * @param hql
	 * @return
	 */
	public List<T> getListByHql(int firstresult, int maxresult, String hql) {
		Query query = this.getManager().createQuery(hql);
		query.setFirstResult(firstresult);
		query.setMaxResults(maxresult);
		List<T> result = query.getResultList();
		return result;
	}

	
	public List<Object[]> getNativeListBySql(int firstresult, int maxresult, String hql) {
		Query query = this.getManager().createNativeQuery(hql);
		query.setFirstResult(firstresult);
		query.setMaxResults(maxresult);
		List<Object[]> result = query.getResultList();
		return result;
	}
	
	public List<Object> getNativeListBySql(String hql) {
		Query query = this.getManager().createQuery(hql);
		List<Object> result = query.getResultList();
		return result;
	}
	
	public int getCountByNativeSql(String sql) {

		Query query = this.getManager().createNativeQuery(sql);
		Object result =  query.getSingleResult();
		int len =tranfer(result);
		return len;
	}
	private int tranfer(Object result){
		int len =0;
		if(result instanceof BigDecimal)
			  len =((BigDecimal)result).intValue();
			if(result instanceof BigInteger)
				  len =((BigInteger)result).intValue();
			if(result instanceof Long)
				  len =((Long)result).intValue();
			return len;
			
	}
	public void saveEntity(T t) {
		this.getManager().persist(t);
	}
	
	public void updateEntity(T t) {
		this.getManager().merge(t);
	}
	
	public void delEntity(T t) {
		this.getManager().remove(this.getManager().merge(t));
	}
	
	public void executeUpdateBySql(String sql) {
		this.getManager().createNativeQuery(sql).executeUpdate();
	}
	
	public List<Object> getNativeList(String sql) {
		Query query = this.getManager().createNativeQuery(sql);
		List<Object> result = query.getResultList();
		return result;
	}
	@Override
	protected Long findId(Object entity) {

		return null;
	}

	@Override
	protected void update(Long id, Object entity) {
		
	}
	
}
